package cn.bdqn.controller;

import cn.bdqn.bean.result.Page;
import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.Accept;
import cn.bdqn.service.AcceptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/accept")
public class AcceptController {

    @Autowired
    private AcceptService acceptService;

    //分页  查询所有的接收信息
    @GetMapping("/queryAll")
    public Result queryAll(){
        Page<Accept> acceptList=acceptService.queryAll(1);
        if (acceptList != null){
            return ResultGenerator.genSuccessResult(acceptList);
        }else {
            return ResultGenerator.genFailResult("未查到信息");
        }
    }

    //分页   根据条件查询接收信息
    @GetMapping("/queryBySelective")
    public Result queryBySelective(Accept accept){
        Page<Accept> acceptList=acceptService.queryBySelective(accept,1);
        if (acceptList != null){
            return ResultGenerator.genSuccessResult(acceptList);
        }else {
            return ResultGenerator.genFailResult("未查到信息");
        }
    }

    //根据id查询接收信息
    @GetMapping("/queryById")
    public Result queryById(Integer id){
        Accept accept=acceptService.queryById(id);
        if (accept != null){
            return ResultGenerator.genSuccessResult(accept);
        }else {
            return ResultGenerator.genFailResult("未查到信息");
        }
    }

    //添加一条接收信息
    @PostMapping("/addSelective")
    public Result addSelective(Accept accept){
        try{
            acceptService.addSelective(accept);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("出现了错误，添加失败");
        }
        return ResultGenerator.genSuccessResult();
    }

    //根据id修改接收信息
    @PostMapping("/change")
    public Result changeByPrimaryKeySelective(Accept accept){
        try{
            acceptService.changeByPrimaryKeySelective(accept);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("出现了错误，修改失败");
        }
        return ResultGenerator.genSuccessResult();
    }

    //根据id删除接收信息
    @PostMapping("/removeById")
    public Result removeById(Integer id){
        try{
            acceptService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("出现了错误,删除失败");
        }
        return ResultGenerator.genSuccessResult();
    }
}
